package com.ykdl.member.kid.adapters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.FeedNotifyBean;

public class NotifyAdapter extends BaseAdapter implements OnCheckedChangeListener{
	
	private List<FeedNotifyBean> list;
	private Context context;
	private SimpleDateFormat format = new SimpleDateFormat("HH:mm");
	
	public NotifyAdapter() {}
	
	public NotifyAdapter(Context context, List<FeedNotifyBean> list) {
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		if (list == null || list.size() == 0) {
			return 0;
		} else {
			return list.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_feed_notify, null);
			holder = new ViewHolder();
			holder.txtTime = (TextView)convertView.findViewById(R.id.txtTime);
			holder.gBtn = (ToggleButton)convertView.findViewById(R.id.tButton);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		FeedNotifyBean bean = (FeedNotifyBean) getItem(position);
		holder.txtTime.setText(format.format(new Date(bean.getTime())));
		holder.gBtn.setChecked(bean.isNotify());
		holder.gBtn.setOnCheckedChangeListener(this);
		holder.gBtn.setTag(bean);
		return convertView;
	}
	
	private class ViewHolder{
		TextView txtTime;
		ToggleButton gBtn;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		((FeedNotifyBean)buttonView.getTag()).setNotify(isChecked);
	}
	
}